/*
 * Supply.cpp
 *
 *  Created on: 09-01-2011
 *      Author: Piotr
 */

#include "world/Supply.hpp"
#include "utils/Randomizer.hpp"
#include "pch.hpp"

const float Supply::SIZE = 10.0f;
const float Supply::DEFAULT_SPOILAGE_STEP = 0.1f;

Supply::Supply() :
	amount(Range<float> (0.0f, static_cast<float> (Randomizer::nextInt(0, 50))),
			static_cast<float> (Randomizer::nextInt(0, 50))), spoilageTime(Range<float> (0.0f,
			static_cast<float> (Randomizer::nextInt(0, 600))), 0.0f)
{
	bounds.setSize(Dimension(SIZE, SIZE));
}

Supply::Supply(float amount, RangedValue<float> spoilageTime) :
	amount(RangedValue<float> (Range<float> (0.0f, amount), amount)), spoilageTime(spoilageTime)
{
	bounds.setSize(Dimension(SIZE, SIZE));
}

Supply::Supply(const Supply& other) :
	amount(other.amount), spoilageTime(other.spoilageTime)
{
	bounds.setSize(Dimension(SIZE, SIZE));
}

Supply::Supply(const Vector& position) :
	amount(Range<float> (0.0f, static_cast<float> (Randomizer::nextInt(0, 50))),
			static_cast<float> (Randomizer::nextInt(0, 50))), spoilageTime(Range<float> (0.0f,
			static_cast<float> (Randomizer::nextInt(0, 600))), 0.0f)
{
	bounds.setPosition(position);
	bounds.setSize(Dimension(SIZE, SIZE));
	setToDelete(false);
}

void Supply::updateWithDeltaTime(float)
{
	spoilageTime.setValue(spoilageTime.getValue() + getSpoilageStep());
	if(spoilageTime.getValue() == spoilageTime.getRange().getMaximum())
	{
		amount.setValue(amount.getRange().getMinimum());
		setToDelete(true);
	}
}

bool Supply::isEmpty() const
{
	return amount.getValue() == amount.getRange().getMinimum();
}

float Supply::getAmount() const
{
	return amount.getValue();
}

void Supply::decreaseAmount(float decreaseAmount)
{
	amount.setValue(amount.getValue() - decreaseAmount);
	if(isEmpty())
	{
		setToDelete(true);
	}
}

const RangedValue<float>& Supply::getSpoilageTime() const
{
	return spoilageTime;
}

float Supply::getSpoilageStep() const
{
	return DEFAULT_SPOILAGE_STEP;
}
